package com.orhon.smartcampus.modules.user.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.orhon.smartcampus.modules.base.mm.LangMapper;
import com.orhon.smartcampus.modules.student.dao.SInformationDao;
import com.orhon.smartcampus.modules.student.entity.SInformation;
import com.orhon.smartcampus.modules.teacher.dao.TInformationDao;
import com.orhon.smartcampus.modules.teacher.entity.TInformation;
import org.mapstruct.factory.Mappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.modules.systemctl.dao.MenusDao;
import com.orhon.smartcampus.modules.systemctl.dao.ModulesDao;
import com.orhon.smartcampus.modules.systemctl.entity.Menus;
import com.orhon.smartcampus.modules.systemctl.entity.Modules;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLMenus;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLModule;
import com.orhon.smartcampus.modules.systemctl.mm.MenuMapper;
import com.orhon.smartcampus.modules.systemctl.mm.MenuSqlMapper;
import com.orhon.smartcampus.modules.systemctl.mm.ModuleMapper;
import com.orhon.smartcampus.modules.systemctl.mm.ModuleSqlMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.modules.user.dao.UsersDao;
import com.orhon.smartcampus.modules.user.entity.Users;
import com.orhon.smartcampus.modules.user.gqlModel.GQLUser;
import com.orhon.smartcampus.modules.user.mm.UserMapper;
import com.orhon.smartcampus.utils.IPInfoUtil;
import com.orhon.smartcampus.utils.IPUtil;
import com.orhon.smartcampus.utils.R;

import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
public class ModuleAndMenuController {

	@Autowired
	UsersDao usersDao;

	@Autowired
	MenusDao menusDao;

	@Autowired
	ModulesDao modulesDao;

	@Autowired
	private TokenService tokenService;

	@Autowired
	SInformationDao sInformationDao;

	@Autowired
	TInformationDao tInformationDao;


	@GetMapping("/systemctl/system/get/user/menus")
	public R getUserMenus() throws Exception {
		List<GQLModule> retData = new ArrayList<>();
		try {
			//String token = (String) SecurityUtils.getSubject().getPrincipal();
			String token = tokenService.getSystemToken();
			if(token==null) {
				return  R.NoLogin();
			}
			Long userid = JwtUtils.getUserId(token);
			if (!token.equals(null)) {
				List<Modules> dataList = modulesDao.find("from Modules where deleted_at=null and id=2", null, 0, 0);
				for (Modules modules : dataList) {

					List<Menus> menus = modules.getMenus();

					List<GQLMenus> gqlMenus = MenuMapper.MAPPER.toMenusList(menus);



					GQLModule module = ModuleMapper.MAPPER.mapModule(modules);

					module.setMenus(gqlMenus);

					retData.add(module);


				}
			}
		}catch (Exception e){
			e.printStackTrace();
		}

		return R.ok().put("data" , retData);
	}


	@GetMapping("/systemctl/system/get/userinfo")
	public R getUserInfo(){
		//String token = (String) SecurityUtils.getSubject().getPrincipal();
		String token = tokenService.getSystemToken();
		if (!token.equals(null)) {
			Long userid = JwtUtils.getUserId(token);
			try {
				Users user = usersDao.findById(userid);
				user.getSchool();
				List<SInformation> sInformationList = sInformationDao.find("from SInformation where deleted_at = null and user_id = "+userid,null);
				if(sInformationList.size() > 0){
					user.setName(sInformationList.get(0).getStudent_name());
				}
				List<TInformation> tInformationList = tInformationDao.find("from TInformation where deleted_at = null and user_id = "+userid,null);
				if(tInformationList.size() > 0){
					user.setName(tInformationList.get(0).getTeacherName());
				}
				GQLUser gqlUser = UserMapper.MAPPER.mapUser(user);
				if(user.getName() != null){
					gqlUser.setName(LangMapper.MAPPER.mapLang(user.getName()));
				}
				return R.ok().put("data" , gqlUser);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				return R.error();
			}
		}
		return R.NoLogin();  
	}

	/**
	 * 登录用户的权限菜单
	 * @return
	 */
	@GetMapping("/get/user/menus/list")
	public R getMenuList(){
		String token = tokenService.getSystemToken();
		if(token==null) {
			return  R.NoLogin();
		}
		String usertype = JwtUtils.getUserType(token);
		Long userid = JwtUtils.getUserId(token);
		Long school_id = JwtUtils.getSchoolId(token);

		List<GQLMenus> ret = new ArrayList<>();
		// 1、用户是学校管理员则返回学校的所有菜单
		if (usertype.equals("school_admin")){
			List menus = menusDao.findSchoolAdminmenus(school_id);
			menus.forEach(item->{
				GQLMenus m = MenuSqlMapper.MAPPER.toMenus((Map)item);
				ret.add(m);
			});
		}
		// 超级管理员
		else if (usertype.equals("admin")){
			List menus = menusDao.findSuperAdminmenus(userid);
			menus.forEach(item->{
				GQLMenus m = MenuSqlMapper.MAPPER.toMenus((Map)item);
				ret.add(m);
			});
		}
		// 2、如果用户是普通老师则只返回给当前用户分配的菜单
		else {
			List menus = menusDao.findBySQL(userid);
			menus.forEach(item->{
				GQLMenus m = MenuSqlMapper.MAPPER.toMenus((Map)item);
				ret.add(m);
			});
		}



		return R.ok().put("data",ret);
	}
	/**
	 * 登录用户的模块列表
	 * @return
	 */
	@GetMapping("/get/user/module/list")
	public R getModuleList(){
		String token = tokenService.getSystemToken();
		if(token==null) {
			return  R.NoLogin();
		}
		String usertype = JwtUtils.getUserType(token);
		Long userid = JwtUtils.getUserId(token);
		Long school_id = JwtUtils.getSchoolId(token);

		List<GQLModule> ret = new ArrayList<>();
		if (usertype.equals("school_admin")){
			List module = menusDao.findSchoolAdminModules(school_id);
			module.forEach(item->{
				GQLModule m = ModuleSqlMapper.MAPPER.toModules((Map)item);
				ret.add(m);
			});
		}
		// 超级管理员
		else if (usertype.equals("admin")){
			List module = menusDao.findSuperAdminModules(userid);
			module.forEach(item->{
				GQLModule m = ModuleSqlMapper.MAPPER.toModules((Map)item);
				ret.add(m);
			});
		}
		// 2、如果用户是普通老师则只返回给当前用户分配的菜单
		else {

			List module = menusDao.findModuleByUserID(userid);
			module.forEach(item->{
				GQLModule m = ModuleSqlMapper.MAPPER.toModules((Map)item);
				ret.add(m);
			});
		}


		return R.ok().put("data",ret);
	}

	@GetMapping(value = "/weather")
	public Object tianqi(HttpServletRequest request) throws Exception {
		
		String ipAddress = IPUtil.getIpAddress(request);
		System.out.println("ip地址======================"+ipAddress);
		String weather = IPInfoUtil.getIpInfo(ipAddress);
		JSONObject parse = (JSONObject) JSONObject.parse(weather);
		String object = parse.get("msg").toString();
		if(!object.equals("success")) {
			System.out.println("天气获取失败默认呼和浩特天气======================"+ipAddress);
			String weather1 = IPInfoUtil.getIpInfo("1.30.177.46");
			Object weathers1 = JSON.parse(weather1);
			return weathers1;
		}
		Object weathers = JSON.parse(weather);
		
		return weathers;
	}



}
